import { Component, OnInit } from '@angular/core';
// 导入表单相关：表单验证、表单构建、表单组
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { EmailService } from 'src/app/core/service/email.service';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-email-config-tab',
  templateUrl: './email-config-tab.component.html',
  styleUrls: ['./email-config-tab.component.scss']
})
export class EmailConfigTabComponent implements OnInit {
  emailConfigForm: FormGroup;
  checked = true;
  defaultValue = 'SMTP';
  info = {};
  isJudge = 0;
  constructor(
    private emailService: EmailService,
    fb: FormBuilder,
    private message: NzMessageService) {
    this.emailConfigForm = fb.group({
      protocol: ['', Validators.required],
      port: ['', Validators.required],
      hostName: ['', Validators.required],
      TLS: [''],
      senderName: ['', Validators.required],
      senderEmail: ['', Validators.compose([Validators.required, Validators.email])],
      senderPassword: ['', Validators.required],
    });
  }
  // 弹框
  createMessage(type: string, ): void {
    if (type === 'success') {
      this.message.create(type, `配置成功!`, { nzDuration: 5000 });
    } else if (type === 'error') {
      this.message.create(type, `表单校验失败!请检查后重新提交`, { nzDuration: 5000 });
    }
  }
  /**
   * 获取邮件配置
   */
  getEmailConfig() {
    this.emailService.getEmailConfig().subscribe(emailData => {
      console.log('=== getEmailConfig().emailData ===>', emailData);
      if (emailData.status === 1) {
        this.isJudge = 1;
        this.getConfigData(emailData.data);
        console.log('=== getEmailConfig().emailData.data ===>', typeof emailData.data);
      }
    });
  }

  getConfigData(data: any): void {
    console.log('=== getConfigData().data ===>', data.port);
    this.emailConfigForm.value.port = data.port;

    this.emailConfigForm.patchValue(
      {
        protocol: 'SMTP',
        port: data.port,
        hostName: data.host,
        TLS: data.tls,
        senderName: data.senderName,
        senderEmail: data.userName,
      });
  }

  ngOnInit() {
    this.getEmailConfig();
    const date = new Date();
    console.log('=== date ===', date.getFullYear);
  }
  // 判断是新增配置还是修改配置
  judgeConfig() {
    if (this.isJudge === 1) {
      this.editConfig();
    } else {
      this.addConfig();
    }

  }
  // 新增邮件配置
  addConfig() {
    // tslint:disable-next-line: forin
    for (const c in this.emailConfigForm.controls) {
      this.emailConfigForm.controls[c].markAsTouched();
    }
    if (this.emailConfigForm.valid) {
      let postData = {
        'host': this.emailConfigForm.value.hostName,
        'port': this.emailConfigForm.value.port,
        'senderName': this.emailConfigForm.value.senderName,
        'userName': this.emailConfigForm.value.senderEmail,
        'password': this.emailConfigForm.value.senderPassword,
        'auth': this.emailConfigForm.value.protocol ? true : false,
        'tls': this.emailConfigForm.value.TLS ? true : false,
        'createTime': (new Date()).getTime(),
        'modifyTime': ''
      }
      console.log("postData", postData);
      this.emailService.addEmailConfig(postData).subscribe(emailData => {
        console.log('==addConfigData==', emailData);
        if (emailData.status === 1) {
          this.createMessage('success');
        } else {
          this.createMessage('error');
        }
      });
    } else {
      this.createMessage('error');
    }
  }
  // 修改配置
  editConfig() {
    // tslint:disable-next-line: forin
    for (const c in this.emailConfigForm.controls) {
      this.emailConfigForm.controls[c].markAsTouched();
    }
    if (this.emailConfigForm.valid) {
      let postData = {
        'host': this.emailConfigForm.value.hostName,
        'port': this.emailConfigForm.value.port,
        'senderName': this.emailConfigForm.value.senderName,
        'userName': this.emailConfigForm.value.senderEmail,
        'password': this.emailConfigForm.value.senderPassword,
        'auth': this.emailConfigForm.value.protocol ? true : false,
        'tls': this.emailConfigForm.value.TLS ? true : false,
        'createTime': '',
        'modifyTime': (new Date()).getTime()
      };
      console.log(this.emailConfigForm);
      console.log('pospostDatat', postData);
      this.emailService.editEmailConfig(postData).subscribe(emailData => {
        console.log('==edtiConfigData==', emailData);
        if (emailData.status === 1) {
          this.createMessage('success');
          this.isJudge = 0;
        } else {
          this.createMessage('error');
        }
      });
    } else {
      this.createMessage('error');
    }
  }
}
